<?php

require_once '../simpletest/autorun.php';

require_once '../CryoBB/Base.php';
require_once '../CryoBB/Tag.php';
require_once '../CryoBB/Tag/Parameter.php';
require_once '../CryoBB/Emoticons.php';
require_once '../CryoBB/Processor.php';
require_once '../CryoBB/Match.php';
require_once '../Demo/processors/Upper.php';

class TestOfBase extends UnitTestCase
{
	// test simple tag
	public function testSimple()
	{
		$b = new CryoBB_Tag('b');
		$b->setOpenTag('<strong>');
		$b->setCloseTag('</strong>');
		
		$base = new CryoBB_Base();
		$base->addTag($b);
		
		$output = $base->make('[b]123abc[/b]');
		
		$this->assertEqual($output, '<strong>123abc</strong>');
	}
	
	// test tag with parameter
	public function testParameter()
	{
		$color = new CryoBB_Tag_Parameter('color');
		$color->setOpenTagParameter('<span style="color: {color};">', '{color}');
		$color->setCloseTag('</span>');
		
		$base = new CryoBB_Base();
		$base->addTag($color);
		
		$output = $base->make('[color=#FF0000]123abc[/color]');
		$this->assertEqual($output, '<span style="color: #FF0000;">123abc</span>');
	}
	
	// test behavior with optional parameter
	public function testParameterOptional()
	{
		$color = new CryoBB_Tag_Parameter('color');
		
		// if parameter is present
		$color->setOpenTagParameter('<span style="color: {color};">', '{color}');
		// if parameter is absent
		$color->setOpenTag('<span style="color: #0000FF;">');
		
		$color->setCloseTag('</span>');
		
		$base = new CryoBB_Base();
		$base->addTag($color);
		
		// assert with parameter
		$output = $base->make('[color=#FF0000]123abc[/color]');
		$this->assertEqual($output, '<span style="color: #FF0000;">123abc</span>');
		
		// assert without parameter
		$output = $base->make('[color]123abc[/color]');
		$this->assertEqual($output, '<span style="color: #0000FF;">123abc</span>');
	}
	
	// test with processor object
	public function testProcessor()
	{
		$upper = new CryoBB_Tag('upper');
		$upper->setProcessor(new Processor_Upper());
		
		$base = new CryoBB_Base();
		$base->addTag($upper);
		
		$output = $base->make('[upper]123abc[/upper]');
		$this->assertEqual($output, '123ABC');
	}
	
	// test with new line
	public function testBr()
	{
		$base = new CryoBB_Base();
		
		$output = $base->make("123abc\n456def");
		$this->assertEqual($output, "123abc<br />\n456def");
	}
	
	// complex test case
	public function testComplex()
	{
		$b = new CryoBB_Tag('b');
		$b->setOpenTag('<strong>');
		$b->setCloseTag('</strong>');
		
		$i = new CryoBB_Tag('i');
		$i->setOpenTag('<em>');
		$i->setCloseTag('</em>');
		
		$color = new CryoBB_Tag_Parameter('color');
		$color->setOpenTag('<span>');
		$color->setOpenTagParameter('<span style="color: {placeholder}">', '{placeholder}');
		$color->setCloseTag('</span>');
		
		$upper = new CryoBB_Tag('upper');
		$upper->setProcessor(new Processor_Upper());
		
		$code = new CryoBB_Tag('code');
		$code->parseContents(false);
		$code->setOpenTag('<pre class="code">');
		$code->setCloseTag('</pre>');
		
		$base = new CryoBB_Base();
		$base->addTags($b, $i, $color, $upper, $code);
		
		$output = $base->make('[b]four [i]score [color=#FF0000]and
[code]twenty [i]years [/i]ago, [/code]our [/color]fathers [upper]brought [/upper]forth [/i]on [/b]this...');
		$this->assertEqual($output, '<strong>four <em>score <span style="color: #FF0000">and<br />
<pre class="code">twenty [i]years [/i]ago, </pre>our </span>fathers BROUGHT forth </em>on </strong>this...');
	}
}